<template>
    <view>
        <custom><template #backText>商家入驻</template></custom>
        <template v-if="page && page.apply_status == 0">
            <emptyList :empty_list_title="page.apply_remark || '已有入驻记录在申请中，请等待...'"></emptyList>
        </template>
        <template v-else-if="page && page.apply_status == 2">
            <emptyList :empty_list_title="page.apply_remark || '入驻申请失败，请重新申请'"></emptyList>
            <nFormButton title="重新申请" :tap="()=>{page.apply_status = -1}"></nFormButton>
        </template>
        <template v-else-if="page && page.apply_status == 1">
            <emptyList :empty_list_title="page.apply_remark || '入驻申请成功，可以继续申请'"></emptyList>
            <nFormButton title="继续申请" :tap="()=>{page.apply_status = -1}"></nFormButton>
        </template>
        <template v-else>
            <view>
                <nFormItem v-if="!sa_id" key="form_is_help_apply" type="switch" title="是否代商家入驻" :data.sync="data" field="is_help_apply"></nFormItem>
                <nFormItem key="form_is_seller_is_firm" type="checkbox_fields" title="商家类型" :data.sync="data"
                    :checkbox_values="{is_seller: '商家', is_firm: '企业'}"></nFormItem>
                <nFormItem key="form_seller_name" title="商家名称" :data.sync="data" field="seller_name"></nFormItem>
                <formProvinceCityArea ref="formProvinceCityArea" title="所在地区" :data.sync="data" field="seller_area_code"></formProvinceCityArea>
                <nFormItem key="form_seller_address_remark" title="详细地址" :data.sync="data" field="seller_address_remark"></nFormItem>
                <template v-if="data.form.is_help_apply != '1'">
                    <nFormItem key="form_seller_contact_phone" title="联系电话" :data.sync="data" field="seller_contact_phone"></nFormItem>
                </template>
            </view>
            <view class="margin-top-10" v-if="data.form.is_help_apply != '1'">
                <nFormItem key="form_account" title="登录账号" placeholder="请填写入驻成功后登录用的账号" :data.sync="data" field="account"></nFormItem>
                <nFormItem key="form_password_md5" title="登录密码" placeholder="请填写入驻成功后登录用的密码" :data.sync="data" field="password_md5"
                    :password="true"></nFormItem>
                <nFormItem key="form_parent_id" title="推荐人邀请码" :data.sync="data" field="parent_id" format="number"></nFormItem>
            </view>
            <view class="margin-top-10" v-if="data.form.is_help_apply != '1'">
                <nFormItem key="form_seller_gold_coupon_ratio" title="金券赠送比例" :data.sync="data" field="seller_gold_coupon_ratio"
                    format="number"></nFormItem>
            </view>
            <view class="margin-top-10">
                <nFormItem key="form_seller_logo" type="image" image_mode="scaleToFill" title="商家正面Logo" value="建议: 宽高1:1 最大像素750*750"
                    :data.sync="data" field="seller_logo"></nFormItem>
                <template v-if="data.form.is_help_apply != '1'">
                    <nFormItem key="form_seller_license" type="image" title="商家营业执照" :data.sync="data" field="seller_license"></nFormItem>
                    <nFormItem key="form_seller_legaler_avatar" type="image" title="身份证头像面" :data.sync="data" field="seller_legaler_avatar"></nFormItem>
                    <nFormItem key="form_seller_legaler_emblem" type="image" title="身份证国徽面" :data.sync="data" field="seller_legaler_emblem"></nFormItem>
                </template>
            </view>
            <nFormButton title="申请入驻" :tap="apply"></nFormButton>
        </template>
    </view>
</template>

<script>
    import formProvinceCityArea from '@/components/nddyny/form/provinceCityArea.vue';
    export default {
        components: {
            formProvinceCityArea
        },
        data() {
            let data_form = {
                // 通用
                is_seller: '1',
                is_firm: '0',
                seller_name: '',
                seller_area_code: '',
                seller_address_remark: '',
                seller_logo: '',

                // 判断是不是帮店铺申请的
                is_help_apply: '',
                help_sa_id: '',
                help_seller_id: '',

                // 自己申请的
                parent_id: '',
                seller_gold_coupon_ratio: '',
                account: '',
                password_md5: '',

                // 自己申请的 / 帮店铺申请的
                seller_contact_phone: '',
                seller_legaler_avatar: '',
                seller_legaler_emblem: '',
                seller_license: '',
            };
            return {
                seller_id: '',
                sa_id: '',
                page: {
                    apply_status: '',
                    apply_remark: '',
                },
                data: {
                    image_info: {
                        seller_logo: '',
                        seller_contact_phone: '',
                        seller_legaler_avatar: '',
                        seller_legaler_emblem: '',
                        seller_license: '',
                    },
                    formtag: {},
                    form: this.$nddyny.object.copy(data_form),
                    formbak: this.$nddyny.object.copy(data_form)
                }
            }
        },
        onLoad(params) {
            if (params.sa_id) {
                this.sa_id = params.sa_id;
                this.seller_id = params.seller_id;
            }
            this.pageInit();
        },
        methods: {
            pageInit() {
                this.$api.post(this, {
                    url: '/seller/apply/query',
                    success: (Result) => {
                        if (this.$api.form.toast(this, Result)) return;
                        this.page = Result.result;
                        if ((!Result.result || (this.page && this.page.apply_status != 0)) && this.sa_id &&
                            this.seller_id) {
                            this.$set(this, 'page', {
                                apply_status: -1,
                                apply_remark: '',
                            })
                            this.$api.post(this, {
                                url: '/seller/info/info',
                                data_key: null,
                                data_append: {
                                    seller_id: this.seller_id
                                },
                                success: (Result, data_key) => {
                                    if (this.$api.form.toast(this, Result, data_key)) return;
                                    let seller_info = Result.result;
                                    this.$set(this.data, 'form', Object.assign(this.data.form, {
                                        is_seller: seller_info.is_seller,
                                        is_firm: seller_info.is_firm,
                                        seller_name: seller_info.name,
                                        seller_area_code: seller_info.area_code,
                                        seller_address_remark: seller_info.address_remark,
                                        seller_logo: seller_info.logo,

                                        help_sa_id: this.sa_id,
                                        help_seller_id: this.seller_id
                                    }))
                                    this.$set(this.data.image_info, 'seller_logo', seller_info.logo);
                                    this.$nextTick(() => {
                                        this.$refs.formProvinceCityArea.setAddressInfo(
                                            seller_info.area_code);
                                    })
                                }
                            });
                        }
                    }
                });
            },
            apply() {
                this.$api.post(this, {
                    loading: '申请入驻中',
                    url: '/seller/apply/apply',
                    upload: {
                        uri: '/seller/apply/upload',
                        fields: ['seller_logo', 'seller_legaler_avatar', 'seller_legaler_emblem',
                            'seller_license'
                        ],
                        info_old: this.data.image_info,
                        info_new: this.data.form
                    },
                    success: (Result) => {
                        if (this.$api.form.toast(this, Result)) return;
                        let is_help_apply = this.data.form.is_help_apply;
                        this.data.form = this.$nddyny.object.copy(this.data.formbak);
                        if (is_help_apply != '1') {
                            this.pageInit();
                            this.$store.commit('userToastSuccess', '申请入驻成功');
                        } else {
                            this.data.form.is_help_apply = '1';
                            this.$store.commit('userToastSuccess', '代申请入驻成功');
                        }
                    }
                });
            },
        }
    }
</script>
